package com.tlkg.qa.redistest;

import com.tlkg.qa.redistest.self.newer.RedisUtil;

import lombok.extern.log4j.Log4j;

@Log4j
public class TestThead2 extends Thread {

	/**
	 * 1 -- set <br/>
	 * 2 -- get
	 */
	private int test = 1;
	private int num;

	public TestThead2(String name) {
		this.setName(name);
	}

	public void run() {

		if (test == 1) {
			for (int i = 1; i <= num; i++) {
				if (i == Math.round(num * 0.1)) {
					log.info("已完成10%：num=" + i);
				} else if (i == Math.round(num * 0.2)) {
					log.info("已完成20%：num=" + i);
				} else if (i == Math.round(num * 0.3)) {
					log.info("已完成30%：num=" + i);
				} else if (i == Math.round(num * 0.4)) {
					log.info("已完成40%：num=" + i);
				} else if (i == Math.round(num * 0.5)) {
					log.info("已完成50%：num=" + i);
				} else if (i == Math.round(num * 0.6)) {
					log.info("已完成60%：num=" + i);
				} else if (i == Math.round(num * 0.7)) {
					log.info("已完成70%：num=" + i);
				} else if (i == Math.round(num * 0.8)) {
					log.info("已完成80%：num=" + i);
				} else if (i == Math.round(num * 0.9)) {
					log.info("已完成90%：num=" + i);
				} else if (i == num) {
					log.info("已完成100%：num=" + i);
				}

				try {
					String set = RedisUtil.set(this.getName() + i, "test_" + this.getName() + i);

					if (!"OK".equals(set)) {
						log.warn("set [" + this.getName() + i + "] false");
						Count.setmisscount.getAndAdd(1);
					} else {
						log.debug(" [" + this.getName() + "-" + i + "] set" + set);
					}
				} catch (Exception e) {
					log.warn("set [" + this.getName() + i + "] false:" + e.getMessage());
					Count.setmisscount.getAndAdd(1);

				}
			}
		}

		if (test == 2) {
			for (int i = 1; i <= num; i++) {
				if (i == Math.round(num * 0.1)) {
					log.info("已完成10%：num=" + i);
				} else if (i == Math.round(num * 0.2)) {
					log.info("已完成20%：num=" + i);
				} else if (i == Math.round(num * 0.3)) {
					log.info("已完成30%：num=" + i);
				} else if (i == Math.round(num * 0.4)) {
					log.info("已完成40%：num=" + i);
				} else if (i == Math.round(num * 0.5)) {
					log.info("已完成50%：num=" + i);
				} else if (i == Math.round(num * 0.6)) {
					log.info("已完成60%：num=" + i);
				} else if (i == Math.round(num * 0.7)) {
					log.info("已完成70%：num=" + i);
				} else if (i == Math.round(num * 0.8)) {
					log.info("已完成80%：num=" + i);
				} else if (i == Math.round(num * 0.9)) {
					log.info("已完成90%：num=" + i);
				} else if (i == num) {
					log.info("已完成100%：num=" + i);
				}

				try {
					String get = RedisUtil.get(this.getName() + i);

					if (null == get) {
						log.warn("get [" + this.getName() + i + "] false");
						Count.getmisscount.getAndAdd(1);
					} else {
						log.debug(" [" + this.getName() + "-" + i + "] - get OK " + get);
					}
				} catch (Exception e) {
					log.warn("get [" + this.getName() + i + "] false:" + e.getMessage());
					Count.getmisscount.getAndAdd(1);
				}
			}
		}
	}

	public void setTest(int test) {
		this.test = test;
	}

	public void setNum(int num) {
		this.num = num;
	}
}
